package controller;

import exception.OtherException;
import exception.SomeException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Auther:ws@itany.com
 * Date:20-9-17 下午3:08
 * Version:1.0
 * Description:
 */
@Controller
@RequestMapping("/exception")
public class ExceptionController {


    @ExceptionHandler(OtherException.class)
    public ModelAndView exceptionHandler(Exception e){
        ModelAndView mav = new ModelAndView();
        mav.addObject("e",e);
        mav.setViewName("exception");
        return mav;
    }

    @ExceptionHandler(SomeException.class)
    public ModelAndView exceptionHandler2(Exception e){
        ModelAndView mav = new ModelAndView();
        mav.addObject("e",e);
        mav.setViewName("exception2");
        return mav;
    }

    @ExceptionHandler
    public ModelAndView exceptionHandlers (Exception e){
        ModelAndView mav = new ModelAndView();
        if(e instanceof SomeException){
            mav.setViewName("exception");
        }
        if(e instanceof OtherException){
            mav.setViewName("exception2");
        }
        return mav;
    }
    @RequestMapping("f1")
    public void f1() throws SomeException{
        System.out.print("Exception.f1");
        throw new SomeException();
    }

    @RequestMapping("f2")
    public void f2() throws OtherException {
        System.out.print("Exception.f1");
        throw new OtherException();
    }
}
